package com.yueliang.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;
import com.yueliang.bean.Key;
import com.yueliang.utils.HttpClientUtil;
import com.yueliang.utils.Json2Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.*;

@Service
public class DeviceServiceImpl implements com.yueliang.service.DeviceService {

    @Value("http://api.heclouds.com")
    private String baseUrl;

    public String getDeviceList() throws IOException {
        String deviceList = HttpClientUtil.get(new JSONObject(), baseUrl + "/devices", new JSONObject());
        return deviceList;
    }

    public JSONObject getDeviceDetailsById(String id) {
        JSONObject jsonObject = null;
        try {
            String deviceDetails = HttpClientUtil.get(new JSONObject(), baseUrl + "/devices/" + id, new JSONObject());
            jsonObject = JSONObject.parseObject(deviceDetails);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }


    public Boolean updateDeviceKey(String id, Key key, Boolean force_offline) throws IOException {
        JSONObject jsonRequestBody = (JSONObject) JSONObject.toJSON(key);
        System.out.println("这是一个Json" + jsonRequestBody);
        String isUpdate = HttpClientUtil.put(new JSONObject(), baseUrl + "/mqtt/v1/devices/" + id + "/key?force_offline=" + force_offline, jsonRequestBody);
        System.out.println(isUpdate);
        return true;
    }

    @Override
    public JSONObject getBase64key() {
        List<String> baseKey = new ArrayList<>();
        baseKey.add("YGpnQGtkR1Y+NCxHKCp4azRec3UiIzJaOiQ4J1FeNHA=");
        baseKey.add("azxaey4uUiw9ZUMrYydLdHN6NllbPSdTb14hS1ROYjA=");
        baseKey.add("fCNQRTldVyt2QDJAQ3I5TEJseXVZY3tgLEFkSHBKWjQ=");
        baseKey.add("XmRpOlFRSnkwRi98YTptZSkvZFo5c0hCKEB3QCROSTs=");
        baseKey.add("NnZsJl9ZUTBTd19NZigpYmAuLjZXRE8jIVVxX2dfc0c=");
        baseKey.add("TDctK1g7RD1LZDcqQ2khV0liQSUzL2dOLTcke0AmTEM=");
        System.out.println(baseKey.get(new Random().nextInt(6)));
        Key key = new Key(baseKey.get(new Random().nextInt(6)));
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(key);
        return jsonObject;
    }

    @Override
    public Boolean deleteDeviceById(String deviceId) throws IOException {
        //调用HttpClient发送put请求
        HttpClientUtil.delete(new JSONObject(), baseUrl + "/mqtt/v1/devices/" + deviceId, new JSONObject());
        return true;
    }

    @Override
    public JSONObject getDeviceImage(String deviceId) throws IOException {
        String s = HttpClientUtil.get(new JSONObject(), baseUrl + "/mqtt/v1/devices/" + deviceId + "/image", new JSONObject());
        JSONObject deviceImage = JSONObject.parseObject(s);
        return deviceImage;
    }

    @Override
    public JSONObject getDeviceData(String deviceId) throws IOException {
        String deviceDetails = HttpClientUtil.get(new JSONObject(), baseUrl + "/devices/" + deviceId + "/datapoints", new JSONObject());
        Map<String, Object> map = Json2Map.json2Map(deviceDetails);
        Map data = (Map) map.get("data");
        List datastreams = (List) data.get("datastreams");
        Map<String, Object> deviceData = new HashMap();
        for (int i = 0; i < datastreams.size(); i++) {
            Map getMap = (Map) datastreams.get(i);
            Object v = getMap.get("id");
            List datapoints = (List) getMap.get("datapoints");
            for (int j = 0; j < datapoints.size(); j++) {
                Map valueList = (Map) datapoints.get(j);
                valueList.put("id", v);
                deviceData.put("value" + i, valueList);
            }
        }
        String valueList = deviceData.toString();
        valueList = valueList.replace('[', '{');
        valueList = valueList.replace(']', '}');
        valueList = valueList.replace('=', ':');
        JSONObject jsonObject = JSONObject.parseObject(valueList);
        return jsonObject;
    }

    public String sendDirective(String deviceId, String content, Integer timeout) throws IOException {
        String responseResult = null;
        try {
            responseResult = HttpClientUtil.postForSendDirective(new JSONObject(), baseUrl + "/v1/synccmds?deviceId=" + deviceId + "&timeout=" + timeout, content);
        } catch (SocketTimeoutException exception) {
            System.out.println(responseResult);
            exception.printStackTrace();
            return responseResult;
        }
        System.out.println("这里是我的第一次传参" + content);
        System.out.println(responseResult);
        return responseResult;
    }
}
